// index.js
// 获取应用实例
const app = getApp()
Page({
  data: {
    baseURL:wx.baseURL,
    count:1,
    path:'/pages/index2/index2',
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    news:[],
    newsIndex:1,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName'), // 如需尝试获取用户信息可改为false
    pageNum:1,
    lists:[],
    total:0,
    loading:'数据加载中'
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },

  inputKey(e){
    console.log(e);
  },

  banner(){
    return new Promise((resolve,reject) => {
      wx.request({
        url: 'https://obuge.com/juzao/index/banners', 
        success: (res) => {
          console.log(res,res.data)
          this.setData({
            bainer:res.data
          }) 
          resolve()
        }
      })
    })
  },

  getIcons(){
    return new Promise((resolve,reject) => {
      wx.request({
        url: 'https://obuge.com/juzao/index/categorys', 
        success: (res) => {
          console.log(res,res.data)
          this.setData({
            headx:res.data
          })
          resolve()
        }
      })
    })
  },

  // 新闻
  getNews() {
    return new Promise((resolve,reject) => {
        wx.$https.get('/index/news').then((res) => {
          this.setData({
            news:res.data
        })
        // this.changeNewsIndex() // 触发新闻轮播
      })
      resolve()
    })
  },

    // 下拉刷新
    onPullDownRefresh() {
      this.data.pageNum = 1
      this.data.lists = []
      this.data.newsIndex = 0
      Promise.all([this.getNews(),this.banner(),this.getIcons(),this.getRecommends()]).then(() => {
        wx.stopPullDownRefresh()
      }).catch(e => {
        wx.stopPullDownRefresh()
      })
      // this.getRecommends()
    },
    
  changeNewsIndex(){//新闻轮播
    // setInterval(() => {
    //   let newsIndex = this.data.newsIndex
    //   newsIndex ++
    //   if(newsIndex == this.data.news.length+1){
    //     newsIndex = 1
    //   }
    //   this.setData({
    //     newsIndex
    //   })
    // }, 1500);
  },

  onReachBottom(){
    if(this.data.total > this.data.lists.length){
      this.data.pageNum ++;
      this.getRecommends()
    }
  },
  getRecommends(){
    return new Promise((resolve,reject) => {
      this.setData({loading:'数据加载中...'})
      wx.request({
        url: 'https://obuge.com/juzao/index/recommends?pageNum=' + this.data.pageNum, //仅为示例，并非真实的接口地址
        success: (res) => {
          console.log(res.data)
          this.setData({
            loading:'已加载完成',
            lists:this.data.lists.concat(res.data.rows),
            total:res.data.total
          })
          resolve()
        }
      })
    })
  },

  onLoad(query) {
    Promise.all([this.getNews(),this.banner(),this.getIcons(),this.getRecommends()]).then(() => {
    this.changeNewsIndex()  
    console.log('所有数据加载完成'); 
    })
  }, 
  onShow() {
    console.log('i am onShow');
  },
  onReady() {
    console.log('i am onReady');
  },
  tapNameBig(event){
    console.log('tapNameBig',event);
    let count = this.data.count
    count++
    this.setData({
      count
    })
  },
  tapName(event) {
    console.log('点击成功',event);
    let url = "/pages/index2/index2"
    wx.showModal({
      cancelColor: 'cancelColor',
      title:'提示',
      // showCancel:false,
      content: '是否加1',
      success:(e) => {
        console.log('i am this2',this);
        let count = this.data.count
        count ++
        this.setData({
          count
        })
      }
      // success(e){
      //   console.log(e);
      //   wx.navigateTo({
      //     url
      //   })
      // }
    })
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})

